package com.tidc.seaserverweb.filter;

import com.google.gson.internal.$Gson$Preconditions;
import org.springframework.dao.CleanupFailureDataAccessException;

import java.util.*;


public class Main {
    public String longestPalindrome(String s) {
        if (s == null || s.length() < 2) {
            return s;
        }
        int strLen = s.length();
        int maxStart = 0;  //最长回文串的起点
        int maxEnd = 0;    //最长回文串的终点
        int maxLen = 1;  //最长回文串的长度
        boolean[][] dp = new boolean[s.length()][s.length()];
        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = true;
        }
//        dp[i][j] =  i == j && dp[i-1][j-1] r-l<=2
        for (int i = 1; i < s.length(); i++) {
            for (int j = 0; j < i; j++) {
                dp[j][i] =  s.charAt(i) == s.charAt(j) && (dp[j-1][i-1] || i-j<=2);
                if (dp[j][i] && i-j+1>maxLen){
                    maxLen = i-j+1;
                    maxStart = j;
                    maxEnd = i;
                }
            }
        }
        return s.substring(maxStart,maxEnd+1);
    }

}
